package com.zaixiandiancan.xia.util;

import net.sf.json.JSONObject;

import java.io.UnsupportedEncodingException;

import com.baidu.translate.demo.TransApi;

public class BaiduFyUtil {
	private static final String APP_ID = "20190408000285709";
	private static final String SECURITY_KEY = "i7Ki6l0fpG44VTllyWZl";

	public static String main(String query, String yy) throws UnsupportedEncodingException {
		TransApi api = new TransApi(APP_ID, SECURITY_KEY);
		String str = api.getTransResult(query, "auto", yy);
		System.out.println(str + "<---这是翻译结果");
		//String转换为Json
		JSONObject json = JSONObject.fromObject(str.toString());
		//大元素取出后转String清理[]后再转Json
		String str1 = json.get("trans_result").toString();
		str1 = str1.replace("[", "");
		str1 = str1.replace("]", "");
		JSONObject json1 = JSONObject.fromObject(str1);
		//调用Json的get方法取出值
		System.out.println("源语言：" + json1.get("src") + "--语言类型" + json.get("from"));
		System.out.println("目标语言：" + json1.get("dst") + "--语言类型" + json.get("to"));
		return json1.get("dst") + "";
	}

}
